/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.pacs.actor;

import akka.actor.ActorRef;
import akka.actor.UntypedActor;
import com.cxhd.pacs.tools.DicomImage;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.Map;

/**
 *
 * @author Administrator
 */
public class DicomToJpgActor extends UntypedActor {

    DicomImage di = new DicomImage();

    @Override
    public void onReceive(Object o) throws Exception {
        Map<String, String> info = (Map) o;
        String targerImagePath = info.get("file_path") + "image/";
          String targerXmlPath = info.get("file_path") + "xml/";
        File f = new File(targerImagePath);
        if (!f.exists()) {
            f.mkdirs();
        }
        f = new File(targerXmlPath);
        if (!f.exists()) {
            f.mkdirs();
        }
        targerImagePath = info.get("file_path") + "/image/";
        targerXmlPath = info.get("file_path") + "/xml/" + info.get("sop_iuid") + ".xml";
        runToJpg(info, targerImagePath);
        runToXml(info, targerXmlPath);
    }

    public void runToJpg(Map<String, String> info, String targerImagePath) {

        try {
            Runtime r = Runtime.getRuntime();

            Process p = r.exec("cmd.exe /c c:/dcmsyst/tools/dcm2jpg.exe -s y -q 50 -o " + targerImagePath + " " + info.get("dicom_path"));
            // System.out.println("cmd.exe /c c:/dcmsyst/tools/dcm2jpg.exe -s y -q 70 -o " + baseDir + "/image/ " + fs.getAbsolutePath());
            p.waitFor();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
//        try {
//            di.saveImage(info.get("dicom_path"), targerImagePath);
//        } catch (Exception ex) {
//            ex.printStackTrace();
//        }
    }

    public void runToXml(Map<String, String> info, String targerXmlPath) {
        try {
            Runtime run = Runtime.getRuntime();
            Process pr = null;
           
            pr = run.exec("c:/dcmsyst/tools/dcm2xml.exe +fo -q +Ca latin-1 " + info.get("dicom_path") + " " + targerXmlPath);
            pr.waitFor();
        } catch (Exception e) {
        }
    }

    public static boolean testPacs(String ip, int port) {
        Socket s = null;
        try {
            s = new Socket(ip, port);
            if (s.isConnected()) {
                return true;
            } else {
                return false;
            }
        } catch (UnknownHostException ex) {
            // ex.printStackTrace();
            System.out.println("cannot connect to " + ip + ":" + port);
            return false;
        } catch (IOException ex) {
            // ex.printStackTrace();
            System.out.println("cannot connect to " + ip + ":" + port);
            return false;
        } finally {
            if (s != null) {
                try {
                    s.close();
                } catch (IOException ex) {
                    // ex.printStackTrace();
                }
            }
        }
    }

}
